% Process a video to generate its feature
% Return feature array
% Arguments:
%    filename: The name of the video file
function [info] = Process(filename)

    % Read the video file and get basic information
    display(filename);
    reader = VideoReader(filename);

    nFrames = reader.NumberOfFrames;
    vidHeight = reader.Height;
    vidWidth = reader.Width;
    
    %only consider first three minutes
    fRate = reader.FrameRate;
    if nFrames>fRate*60*3
        nFrames = fRate*60*3;
    end

    display(nFrames);
    
    % Calculate Neighbor pixels color distance & Region color distance
    % Initialize an image object to store its data
    cur_image = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
    % Add colorspace library
    addpath('colorspace')

    info = [];
    nSample = 7; % Number of selected frames
    frameMult = idivide(int32(nFrames), nSample, 'floor');
    each = 2;
    for k = 1 : nSample
        % Get the kth chosen frame
        frameId = k * frameMult;
        cur_image.cdata = read(reader, frameId);

        % Calculate color distance
        cieluv = colorspace('RGB->Luv', cur_image.cdata);
        tmp = [colordist(cieluv, vidHeight, vidWidth) colordist2(cieluv, vidHeight, vidWidth)];
        info = [info tmp];
        each = size(tmp, 2);
    end
    % Format the returned result
    info = reshape(info, int32(each), []);
    info = info(:)';

    % Calculate Frequency of top 4 colors, change ratio and HSV compression
    n = 10; % Number of selected frames
    
    hsvcomp = zeros(1,35);
    fretop = zeros(1,n);
    chRatio = zeros(1,n);
    for i = 1:n
        % Get the i-th chosen frame
        index = i*idivide(int32(nFrames), n+1, 'floor');
        frame = read(reader,index);

        % Calculate frequency of top 4 colors
        fretop(1,i) = FreTop4(frame);
        
        % Calculate change ratio between 2 frames
        frame2 = read(reader, index+20);
        chRatio(1,i) = ChangeRatio(frame,frame2);
        
        % Calculate hsv features and compression ratio
        cur = [ProcessHSV(frame) CompRatio(frame)]; 
        hsvcomp = hsvcomp+cur;
    end
    hsvcomp = hsvcomp/n;
    info = [info mean(fretop) std(fretop) mean(chRatio) std(chRatio) hsvcomp];
